package com.db;

import java.net.UnknownHostException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.mongodb.BasicDBObject;
import com.mongodb.DB;
import com.mongodb.DBCollection;
import com.mongodb.DBObject;
import com.mongodb.Mongo;
import com.mongodb.MongoException;

public class MongoDb {

	private static Mongo mongo;
	private static DB db;

	/**
	 * 
	 * 
	 * @param msg
	 * @return
	 */
	public static void remove(Map msg) {
		DBObject _a = new BasicDBObject(msg);
		DBCollection coll2 = db.getCollection((String) msg.get("_table"));
		msg.remove("_table");
		DBObject a = new BasicDBObject(msg);
		coll2.remove(a);
	}

	public static void removeByKey(String table, String key) {
		DBObject _a = new BasicDBObject();
		_a.put("key", key);
		DBCollection coll2 = db.getCollection(table);
		coll2.remove(_a);
	}

	/**
	 * update msg
	 * 
	 * @param msg
	 * @return
	 */
	@SuppressWarnings("rawtypes")
	public static DBObject update(Map q, DBObject o) {
		DBObject _a = new BasicDBObject(q);
		DBCollection coll2 = db.getCollection((String) q.get("_table"));
		q.remove("_table");
		DBObject a = new BasicDBObject(q);
		return (DBObject) coll2.update(a, o);
	}

	/**
	 * get a DBObject from a request
	 * 
	 * @param msg
	 * @return
	 */
	public static DBObject findOne(Map msg) {
		DBObject _a = new BasicDBObject(msg);
		DBCollection coll2 = db.getCollection((String) msg.get("_table"));
		msg.remove("_table");
		DBObject a = new BasicDBObject(msg);
		return coll2.findOne(a);

	}

	/**
	 * select the count of the table
	 * 
	 * @param msg
	 * @return
	 */
	public static long count(Map msg) {
		DBObject _a = new BasicDBObject(msg);
		DBCollection coll2 = db.getCollection((String) msg.get("_table"));
		msg.remove("_table");
		DBObject a = new BasicDBObject(msg);
		return coll2.find(a).count();

	}

	/**
	 * get the list like a page
	 * 
	 * @param msg
	 * @param skip
	 * @param limit
	 * @return
	 */
	public static List findAll(Map msg, int skip, int limit) {
		DBObject _a = new BasicDBObject(msg);
		DBCollection coll2 = db.getCollection((String) msg.get("_table"));
		return coll2.find().skip(skip).limit(limit).toArray();

	}

	/**
	 * get all message from the table
	 * 
	 * @param msg
	 * @return
	 */
	public static List findAll(Map msg) {
		DBObject _a = new BasicDBObject(msg);
		DBCollection coll2 = db.getCollection((String) msg.get("_table"));
		_a.removeField("table");
		return coll2.find(_a).toArray();

	}

	/**
	 * insert message into db
	 * 
	 * @param msg
	 */
	public static void insert(Map msg) {

		DBObject _a = new BasicDBObject(msg);
		DBCollection coll2 = db.getCollection((String) msg.get("_table"));
		msg.remove("_table");
		coll2.insert(_a);

	}

	/**
	 * init mongodb object
	 */
	static {
		init();
	}

	/**
	 * init mian function
	 */
	private static void init() {
		try {
			mongo = getMongo();
			db = getDb();
		} catch (MongoException e) {
			e.printStackTrace();
		}

	}

	/**
	 * get the DB object
	 * 
	 * @return
	 */
	public static DB getDb() {
		if (db == null) {
			return mongo.getDB(Config.dbname);
		} else {
			return db;
		}
	}

	/**
	 * 
	 * @return
	 */
	public static Mongo getMongo() {
		if (mongo != null) {
			return mongo;
		}
		try {
			return new Mongo(Config.dbip, Config.dbport);
		} catch (UnknownHostException e) {
			e.printStackTrace();
			return null;
		} catch (MongoException e) {
			e.printStackTrace();
			return null;
		}
	}

	public static List<DBObject> readMeaage(DBCollection coll, int limit,
			int spirt) {
		return coll.find().skip(spirt).limit(limit).toArray();
	}

	public static List<DBObject> readMeaage(DBCollection coll, int limit,
			int spirt, DBObject where) {
		return coll.find(where).skip(spirt).limit(limit).toArray();
	}

	public static DBObject readOneMessage(DBCollection coll, DBObject where) {
		return coll.findOne(where);
	}

	public DBCollection getColl(DB db, String tablename) {
		return db.getCollection(tablename);
	}

	public Mongo getMongo(String path, int port) {
		try {
			return new Mongo(path, port);
		} catch (UnknownHostException e) {
			e.printStackTrace();
			return null;
		} catch (MongoException e) {
			e.printStackTrace();
			return null;
		}
	}
}
